import { http } from '@/utils'
import differenceBy from 'lodash/differenceBy'
const CHANNEL_KEY = 'geek-app-87-home-channel'

// 获取频道
const getMyChannels = () => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState()
    // console.log(token);
    if (token) {
      const res = await http.get('/user/channels')
      const { channels } = res.data.data
      // console.log(channels)
      dispatch({ type: 'home/getMyChannels', payload: channels })
    } else {
      const localChannels = JSON.parse(localStorage.getItem(CHANNEL_KEY))
      if (localChannels) {
        dispatch({ type: 'home/getMyChannels', payload: localChannels })
      } else {
        const res = await http.get('/user/channels')
        const { channels } = res.data.data
        dispatch({ type: 'home/getMyChannels', payload: channels })
        localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels))
      }
    }
  }
}

// 获取所有的频道数据
const getChannels = () => {
  return async (dispatch, getState) => {
    // console.log('getChannels')
    const res = await http.get('/channels')
    const { channels: allChannels } = res.data.data
    // console.log('所有频道数据：', res)
    // 从 所有频道列表数据 中过滤出 我的频道 中没有的数据，再存储到 redux 中
    const {
      home: { channels },
    } = getState()

    // 从 allChannels 数组中排除掉 channels 中的数据
    const restChannels = differenceBy(allChannels, channels, 'id')
    // console.log(restChannles)
    dispatch({ type: 'home/getRestChannels', payload: restChannels })
  }
}

// 添加频道
const addChannel = channel => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState()
    if (token) {
      // 登录
      // 修改接口中的数据
      await http.patch('/user/channels', {
        channels: [{ id: channel.id }],
      })

      // // 更新redux状态，让页面更新
      // dispatch({ type: 'home/addChannel', payload: channel.id })
    } else {
      // 未登录
      // 修改本地存储中的数据
      const myChannels = JSON.parse(localStorage.getItem(CHANNEL_KEY))

      // console.log('本地的 channel', myChannels)

      // 因为本地存储中，频道数据存的是 我的频道 数据，
      // 往本地存储中的 频道数据 中添加一项
      // 而本地没有存储 推荐频道 数据的，所以，不需要再本地存储中对推荐频道数据做处理
      // 所以，推荐频道数据的处理只在 redux 中更新即可
      const newMyChannels = [...myChannels, channel]
      // 将处理后的新我的频道，更新到本地存储中
      localStorage.setItem(CHANNEL_KEY, JSON.stringify(newMyChannels))

      // // 更新redux状态，让页面更新
      // dispatch({ type: 'home/addChannel', payload: channel.id })
    }

    // 更新redux状态，让页面更新
    dispatch({ type: 'home/addChannel', payload: channel.id })
  }
}

// 删除频道
const deleteChannel = channel => {
  return async (dispatch, getState) => {
    // 从login的action状态获取token
    const {
      login: { token },
    } = getState()
    if (token) {
      await http.delete(`/user/channels/${channel.id}`)
    } else {
      const myChannels = JSON.parse(localStorage.getItem(CHANNEL_KEY))
      const newMyChannels = myChannels.filter(item => item.id !== channel.id)
      localStorage.setItem(CHANNEL_KEY, JSON.stringify(newMyChannels))
    }
    dispatch({ type: 'home/deleteChannel', payload: channel })
  }
}

// 切换频道
const selectTab = payload => ({
  type: 'home/selectTab',
  payload,
})

// 获取文章
const getArticles = (channelId, timestamp) => {
  return async dispatch => {
    const res = await http.get('/articles', {
      params: {
        channel_id: channelId,
        timestamp,
      },
    })
    const { pre_timestamp: prevTime, results: list } = res.data.data
    dispatch({ type: 'home/getArticles', payload: { prevTime, list, channelId } })
  }
}

// 到底刷新获取文章数据
const getMoreArticles = (channelId, timestamp) => {
  return async dispatch => {
    const res = await http.get('/articles', {
      params: {
        channel_id: channelId,
        timestamp,
      },
    })
    const { pre_timestamp: prevTime, results: list } = res.data.data
    dispatch({ type: 'home/getMoreArticles', payload: { prevTime, list, channelId } })
  }
}
export {
  getMyChannels,
  getChannels,
  addChannel,
  deleteChannel,
  selectTab,
  getArticles,
  getMoreArticles,
}
